<?php
App::import('Core', 'XmlNode');
App::import('Core', 'Xml');
App::import('Core', 'File');

class ReportsController extends AppController {

	var $name = 'Reports';
	var $uses = array('Procedure', 'Requirement', 'Step') ;

	function index()
	{
		$this->set('isProcedureList', false);


		$this->set('walle', $this);
		if (!empty($this->data)){
			if ($this->data['Report']['type_rapport'] == 'ProcedureList'){
				$this->set('isProcedureList', true);
			}

			if (!empty($this->data['Report']['Document'])){
				$this->redirect($this->data['Report']['type_rapport'].'/'.$this->data['Report']['Document'].'/'.$this->data['Report']['Issue'].'/'.$this->data['Report']['Date'].'/'.$this->data['Report']['foot_left'].'/'.$this->data['Report']['foot_center'].'/'.$this->data['Report']['foot_right']);
			}
		}
	}

	function index_procedureList(){

	}

	function procedureList_html() {
		$doc = $this->data['Report']['Document'] ;
		$issue = $this->data['Report']['Issue'] ;
		$date = $this->data['Report']['Date'] ;
		$foot_left = $this->data['Report']['foot_left'] ;
		$foot_center = $this->data['Report']['foot_center'] ;
		$foot_right = $this->data['Report']['foot_right'] ;

		//		Récupération des infos :
		$procedures = $this->Procedure->findAll(array('proc_project' => $this->Session->read('id_projet')));
				$this->set('walle', $procedures);
		//		$this->set('walle', $this);


		//		print_r($this);
		//		$varxml = new XmlNode('test') ;

		$xml = new Xml();

		$Document = $xml->createNode('document');
		$msg_conf = $Document->createNode('msg_confidentiel', $this->data['Report']['msg_confidentiel']);
		$Header = $Document->createNode('header');
		$Logo = $Header->createNode('logo', $procedures[0]['Project']['proj_main_logo']);
		$RefDoc = $Header->createNode('refDoc', $doc);
		$Issue = $Header->createNode('issue', $issue); //Saisie
		$Date = $Header->createNode('date', $date); //Saisie




		//Proc :
		$ProcedureList = $Document->createNode('procedureList');
		foreach ($procedures as $procedure ){
			//FOReach $Procedure
			$Procedure = $ProcedureList->createNode('procedure');

			$InfoProc = $Procedure->createNode('infoProc');
			$RefProc = $InfoProc->createNode('refProc', $procedure['Procedure']['proc_reference']);
			//			$RefProc = $Header->createNode('RefProc', $procedure['Procedure']['proc_reference']);
			$Applicatibility = $InfoProc->createNode('applicatibility', 'TODO'); //OFF
			$Part = $InfoProc->createNode('part', $procedure['Part']['part_name']);
			$Phase = $InfoProc->createNode('phase', $procedure['Phase']['phase_name']);
			$Duration = $InfoProc->createNode('duration', $procedure['Procedure']['proc_duration']);
			$Version = $InfoProc->createNode('version', $procedure['Procedure']['proc_version']);
			$Title = $InfoProc->createNode('title', $procedure['Procedure']['proc_title']);
			$Scope = $InfoProc->createNode('scope', $procedure['Procedure']['proc_scope']);
			$Prerequisites = $InfoProc->createNode('prerequisites', $procedure['Procedure']['proc_prerequisites']);
			$ProcResult = $InfoProc->createNode('procResult', 'TODO'); //OFF
			$DateProcResult = $InfoProc->createNode('dateProcResult', $procedure['Procedure']['proc_updated_date']);
			$Attendees = $InfoProc->createNode('attendees', $procedure['Procedure']['proc_attendees']);
			$Remarks = $InfoProc->createNode('remarks', $procedure['Procedure']['proc_remarks']);


			//Req :
			$RequirementList = $Procedure->createNode('requirementList');
			//FOReach $Requirement
			foreach($procedure['ProcedureRequirement'] as $procReq){
				$current_req = $this->Requirement->read(null, $procReq['pra_requirement']);


				$Requirement = $RequirementList->createNode('requirement');
				$current_step_req = null;
				if ($current_req['StepRequirement'] != null){
					foreach($current_req['StepRequirement'] as $index => $step_req){
						$current_step_req = $this->Step->read(null, $step_req['sra_step']);
						$reqStep = $Requirement->createNode('reqStep', $current_step_req['Step']['step_number']);
					}
				}

//								$this->set('walle', $current_step_req);
				$ReqRef = $Requirement->createNode('reqRef', $current_req['Requirement']['r_reference']);
				$ReqDescription = $Requirement->createNode('reqDescription', $current_req['Requirement']['r_description']);
			}


			//Steps:
			$StepList = $Procedure->createNode('stepList');
			foreach($procedure['Step'] as $current_step){

				$Step = $StepList->createNode('step');
				$reqRefList = $this->Requirement->query('SELECT R1.r_reference from requirements R1 WHERE R1.r_index IN (SELECT sra_req from step_requirements WHERE sra_step = '.$current_step['step_index'].')');
				foreach ($reqRefList as $index => $req){
					$reqRef = $Step->createNode('reqRef', $req['R1']['r_reference'] );
				}
				
//				$this->set('walle', $reqRefList);
				$step_number = $Step->createNode('step_number', $current_step['step_number']);
				$step_action = $Step->createNode('step_action', $current_step['step_action']);
				$step_result = $Step->createNode('step_result', $current_step['step_result']);
			}
		}
		//Footer :
		$Footer = $Document->createNode('footer');

		$Footer_logo = $Footer->createNode('foot_logo', $procedures[0]['Project']['proj_alt_logo']);
		$Footer_left = $Footer->createNode('foot_left', $foot_left);
		$Footer_center = $Footer->createNode('foot_center', $foot_center);
		$Footer_right = $Footer->createNode('foot_right', $foot_right);


		$input = '<' . '?xml version="1.0" encoding="UTF-8" ?' . '> '.
				 '<?xml-stylesheet type="text/xsl" href="ProcedureList.xsl"?>'.
				 '<!DOCTYPE document SYSTEM "ProcedureList.dtd">'	;

		$fic_xml = new File('./files/ProcedureList.xml');
		$fic_xml->create();
		$fic_xml->write($input);
		$fic_xml->write($xml);

		// Nouvelle instance
		$xslt = new XSLTProcessor();

		// Import de la feuille XSL directement avec simplexml
		$xslt -> importStylesheet(simplexml_load_file('./files/ProcedureList.xsl'));

		// Transformation et enregistrement du résultat dans le fichier save.xml
		// Le fichier XML est également chargé via simplexml
		$xslt -> transformToUri(simplexml_load_file('./files/ProcedureList.xml') , './files/ProcedureList.html');


		$this->redirect('../files/ProcedureList.html');
	}


	function procedureList_pdf() {
//		if(null === $id)
//		{
//			$this->redirect($this->referer());
//		}
//
//		$order = $this->Order->findById(intval($id));
//
//		if(empty($order))
//		{
//			$this->redirect($this->referer());
//		}
//
//		if($order["User"]["id"] !== $this->loggedUserId)
//		{
//			$this->redirect($this->referer());
//		}

//		vendor("pdf/fpdf");
		App::import('Vendor', 'pdf/fpdf');
		$this->layout = "pdf_layout";
//		$this->set("order", $order);

		
	}

}
?>
